package arch.dao.impl.archive;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import arch.dao.archive.OfficialWordDao;
import arch.domain.archive.OfficialWord;

@Repository("officialWordDao")
public class OfficialWordImpl implements OfficialWordDao {
	@Resource private JdbcTemplate jdbcTemplate;

	public Integer query(String sql, Object... objects) {
		return this.jdbcTemplate.queryForInt(sql,objects);
	}

	@SuppressWarnings("unchecked")
	public List<OfficialWord> queryList(String sql, boolean retention,
			boolean archiveNumberDept, boolean parent, Object... objects) {
		return this.jdbcTemplate.query(sql,objects, new OfficialWordMapper(retention,archiveNumberDept,parent));
	}

	public OfficialWord find(Serializable primaryKey) {
		String sql = "select ID, NAME, RANK, RETENTION_ID, ARCHNUMBER_DEPT_ID, OPEN, "
			+"  PARENT_ID, ENABLE, DESCRIPTION from AR_OFFICIALWORD where ID = ? ";
		
		try {
			return (OfficialWord)this.jdbcTemplate.queryForObject(sql, new Object[]{primaryKey},new OfficialWordMapper());
		} catch (EmptyResultDataAccessException e) {
			return null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void merge(OfficialWord entity) {
		String sql = "update AR_OFFICIALWORD set NAME = ?, RANK = ? , RETENTION_ID = ? , ARCHNUMBER_DEPT_ID = ? ,"
			+"OPEN = ? ,PARENT_ID = ?, ENABLE = ?, DESCRIPTION = ?  where ID = ? ";
		
		String open = null;
		if(entity.getOpen()!=null)
			open = entity.getOpen()?"1":"0";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getName(),
				entity.getRank(),
				entity.getRetention()==null?null:entity.getRetention().getId(),
				entity.getArchiveNumberDept() == null? null:entity.getArchiveNumberDept().getDeptId(),
				open,
				entity.getParent()==null?null:entity.getParent().getId(),
				entity.getEnable()==null?"0":(entity.getEnable()?"1":"0"),
				entity.getDescription(),
				entity.getId()
		});
	}

	public void persist(OfficialWord entity) {
		String sql = "insert into AR_OFFICIALWORD (ID, NAME, RANK, RETENTION_ID, ARCHNUMBER_DEPT_ID, OPEN, "
			+" PARENT_ID, ENABLE, DESCRIPTION ) values (?,?,?,?,?,?,?,?,?)";
		
		String open = null;
		if(entity.getOpen()!=null)
			open = entity.getOpen()?"1":"0";
		
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getId(),
				entity.getName(),
				entity.getRank(),
				entity.getRetention()==null?null:entity.getRetention().getId(),
				entity.getArchiveNumberDept() == null? null:entity.getArchiveNumberDept().getDeptId(),
				open,
				entity.getParent()==null?null:entity.getParent().getId(),
				entity.getEnable()==null?"0":(entity.getEnable()?"1":"0"),
				entity.getDescription()
		});
	}
	public void remove(Serializable primaryKey) {
		String sql = "delete from AR_OFFICIALWORD where ID = ? ";

		this.jdbcTemplate.update(sql,new Object[]{primaryKey});
	}
	/**
	 * @author Xin Huang
	 */
	public void batchUpdate(String sql, BatchPreparedStatementSetter setter) {
		if (sql != null && !"".equals(sql.trim()) && setter != null) {
			jdbcTemplate.batchUpdate(sql, setter);
		}
	}
}
